package com.example.intermediate_control;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.intermediate_control.util.ViewUtil;

//寻回密码
public class RetrievedActivity extends AppCompatActivity implements View.OnFocusChangeListener, View.OnClickListener {


    private Button btnBackActivity;
    private EditText etNewPassword;
    private EditText etConfirmPassword;
    private Button btnSubmit;
    private String desc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieved);
        btnBackActivity = findViewById(R.id.btn_backActivity);
        btnBackActivity.setOnClickListener(view -> {
            finish();
        });
        etNewPassword = findViewById(R.id.et_newPassword);
        etNewPassword.addTextChangedListener(new HighTextWatcher(etNewPassword,6));
        etConfirmPassword = findViewById(R.id.et_confirmPassword);
        etConfirmPassword.setOnFocusChangeListener(this);
        etConfirmPassword.addTextChangedListener(new HighTextWatcher(etConfirmPassword,6));
        btnSubmit = findViewById(R.id.btn_submitNewPassword);
        btnSubmit.setOnClickListener(this);

    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        desc = etNewPassword.getText().toString();
        if (hasFocus) {
            if (desc.length() < 6) {
                Toast.makeText(this, "请输入6位密码", Toast.LENGTH_SHORT).show();
                etNewPassword.requestFocus();
            }
        }
    }

    @Override
    public void onClick(View view) {
        Bundle bundle = getIntent().getExtras();
        String phone = bundle.getString("phone");
        String desc2 = etConfirmPassword.getText().toString();
        if (!desc.equals(desc2)){
            Toast.makeText(this, "前后密码不一致,请再次核对", Toast.LENGTH_SHORT).show();
            return;
        }else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("尊敬的"+phone+"用户");
            builder.setMessage("密码修改成功,点击成功返回上一界面,再次修改请点击再次修改");
            builder.setPositiveButton("成功", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
            builder.setNegativeButton("再次修改",null);
//            bundle.putString("newPassword",desc);
            Intent intent = new Intent();
            intent.putExtra("newPassword",desc);
            setResult(Activity.RESULT_OK,intent);
            AlertDialog dialog = builder.create();
            dialog.show();

        }
    }

    private class HighTextWatcher implements TextWatcher {
        private  EditText mView;
        private  Integer mMaxlength;

        private HighTextWatcher(EditText view,Integer mMaxlength){
            this.mView = view;
            this.mMaxlength = mMaxlength;
        }
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String s = editable.toString();
            // 输入文本达到6位(如登录密码)时关闭输入法
            if ( s.length() == 6 && mMaxlength == 6) {
                ViewUtil.hideOneInputMethod(RetrievedActivity.this, mView);
            }
        }
    }
}